#include "ShotCutClip.h"

VideoClip::VideoClip(const JsonVideoClip& JsonData)
{
    for(JsonVideoClip::VideoClipInfo* VideoInfo : JsonData.JsonVideoClipArray)
    {
        VideoClipArray.push_back(make_pair(shared_ptr<JsonVideoClip::VideoClipInfo>(VideoInfo), make_shared<Clip>(VideoInfo->File)));
    }
    
    SetProPerty();
}

void VideoClip::SetProPerty()
{
    for(auto VideoClipInfoItem : VideoClipArray)
    {
        double VideoSpeed =  VideoClipInfoItem.first->ZoomRate;
        VideoClipInfoItem.second->Layer(VideoClipInfoItem.first->Layer);
        VideoClipInfoItem.second->Position(VideoClipInfoItem.first->PositionTime);
        VideoClipInfoItem.second->Start(VideoSpeed * VideoClipInfoItem.first->BeginTime);
        VideoClipInfoItem.second->End(VideoSpeed * VideoClipInfoItem.first->EndTime);
        VideoClipInfoItem.second->time.AddPoint(0, 0, openshot::LINEAR);
        VideoClipInfoItem.second->time.AddPoint(VideoClipInfoItem.first->VideoFrame * VideoSpeed, VideoClipInfoItem.first->VideoFrame, openshot::LINEAR);
        VideoClipInfoItem.second->has_video = VideoClipInfoItem.first->Hasvideo;
        VideoClipInfoItem.second->has_audio = VideoClipInfoItem.first->HasAudio;
    }
}

AudioClip::AudioClip(const JsonAudioClip& JsonData)
{
    for(JsonAudioClip::AudioClipInfo* AudioInfo : JsonData.JsonAudioClipArray)
    {
        AudioClipArray.push_back(make_pair(shared_ptr<JsonAudioClip::AudioClipInfo>(AudioInfo), make_shared<Clip>(AudioInfo->File)));
    }
    
    SetProPerty();
}

void AudioClip::SetProPerty()
{
    for(auto AudioClipItem : AudioClipArray)
    {
        double AudioSpeed =  AudioClipItem.first->ZoomRate;
        AudioClipItem.second->Layer(AudioClipItem.first->Layer);
        AudioClipItem.second->Position(AudioClipItem.first->PositionTime);
        AudioClipItem.second->Start(AudioSpeed * AudioClipItem.first->BeginTime);
        AudioClipItem.second->End(AudioSpeed * AudioClipItem.first->EndTime);
        AudioClipItem.second->time.AddPoint(0, 0, openshot::LINEAR);
        AudioClipItem.second->time.AddPoint(AudioClipItem.first->AudioFrame * AudioSpeed, AudioClipItem.first->AudioFrame, openshot::LINEAR);
        AudioClipItem.second->has_audio = AudioClipItem.first->HasAudio;
    }
}

VideoEffectClip::VideoEffectClip(const JsonVideoEffectClip& JsonData)
{
    for(JsonVideoEffectClip::VideoEffectClipInfo* VideoInfo : JsonData.JsonVideoEffectClipArray)
    {
        VideoEffectClipArray.push_back(make_pair(shared_ptr<JsonVideoEffectClip::VideoEffectClipInfo>(VideoInfo), make_shared<Clip>(VideoInfo->File)));
    }
    
    SetProPerty();
}

void VideoEffectClip::SetProPerty()
{
    std::shared_ptr<GlobalData> Global = GlobalData::GetGlobalData();
    float SrcVideoWidth = Global->GetVideoWidth(), SrcVideoHeight = Global->GetVideoHeight();
    
    for(auto VideoEffectClipItem : VideoEffectClipArray)
    {
        double VideoSpeed =  VideoEffectClipItem.first->ZoomRate;
        double VideoLengthRate =  VideoEffectClipItem.first->VideoLenghtRate;
        float BeginTime = VideoEffectClipItem.first->BeginTime;
        float EndTime = VideoEffectClipItem.first->EndTime;
        float Fps = VideoEffectClipItem.first->Fps;
        
        VideoEffectClipItem.second->Layer(VideoEffectClipItem.first->Layer);
        VideoEffectClipItem.second->Position(VideoEffectClipItem.first->PositionTime);
        
        if(VideoEffectClipItem.first->IsRepeat)
        {
            float RepeatFrame = (EndTime - BeginTime) * Fps * VideoSpeed;
            int RepeatNumber = VideoLengthRate / VideoSpeed;
            float RepeatEndFrame = 0;

            VideoEffectClipItem.second->Start(0);
            VideoEffectClipItem.second->End(VideoLengthRate * (EndTime - BeginTime));
            
            for(int i = 0; i < RepeatNumber + 1; i++)
            {
                float RepeatBeginFrame = RepeatEndFrame + 1;
                VideoEffectClipItem.second->time.AddPoint(RepeatBeginFrame, BeginTime * Fps, openshot::LINEAR);
                RepeatEndFrame = RepeatEndFrame + RepeatFrame;
                VideoEffectClipItem.second->time.AddPoint(RepeatEndFrame, EndTime * Fps, openshot::LINEAR);
            }
        }
        else
        {
            VideoEffectClipItem.second->Start(VideoLengthRate * BeginTime);
            VideoEffectClipItem.second->End(VideoLengthRate * EndTime);
            VideoEffectClipItem.second->time.AddPoint(0, 0, openshot::LINEAR);
            VideoEffectClipItem.second->time.AddPoint(VideoEffectClipItem.first->VideoFrame * VideoLengthRate, VideoEffectClipItem.first->VideoFrame, openshot::LINEAR);
        }
        
        VideoEffectClipItem.second->has_video = VideoEffectClipItem.first->Hasvideo;
        VideoEffectClipItem.second->has_audio = VideoEffectClipItem.first->HasAudio;
        VideoEffectClipItem.second->scale_x = VideoEffectClipItem.first->ScaleX;
        VideoEffectClipItem.second->scale_y = VideoEffectClipItem.first->ScaleY;
        VideoEffectClipItem.second->rotation = VideoEffectClipItem.first->Rotation;
        VideoEffectClipItem.second->location_x = VideoEffectClipItem.first->CoordX / SrcVideoWidth - 0.5f;
        VideoEffectClipItem.second->location_y = VideoEffectClipItem.first->CoordY / SrcVideoHeight - 0.5f;
        VideoEffectClipItem.second->alpha = VideoEffectClipItem.first->Alpha;
    }
}


